#include "stdafx.h"
#include "Matrix2d.h"
#include "Vector2d.h"
#include "GeomUtils.h"

using namespace stl;

//////////////////////////////////////////////////////////////

CMatrix2D::CMatrix2D()
{
	for (int i = 0; i < 3; ++i)
	{
		double dValue = 0.0;
		for (int j = 0; j < 3; ++j)
		{
			if (i == j)
				A[i][j] = 1.0;

			A[i][j] = 0.0;
		}
	}
}

CMatrix2D::CMatrix2D(const MATRIX2D & matrix)
{
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; ++j)
		{
			A[i][j] = matrix.A[i][j];
		}
	}
}

CMatrix2D::CMatrix2D(const double * pMatrix)
{
	//IdenticalMatrix(A);
	if (pMatrix != nullptr)
	{
		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; ++j)
			{
				A[i][j] = pMatrix[i * 3 + j];
			}
		}
	}
}

CMatrix2D::~CMatrix2D()
{
}

CMatrix2D CMatrix2D::operator*(const MATRIX2D & matrix2) const
{
	CMatrix2D matrix;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			matrix.A[i][j] = A[i][0] * matrix2.A[0][j]
				+ A[i][1] * matrix2.A[1][j]
				+ A[i][2] * matrix2.A[2][j];
		}
	}
	return matrix;
}

void CMatrix2D::operator*=(const MATRIX2D & matrix)
{
	(*this) = (*this)*matrix;
}

void CMatrix2D::IdeticalMatrix()
{
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			if (i == j)
				A[i][j] = 1.0;
			else
				A[i][j] = 0.0;
		}
	}
}

double CMatrix2D::GetValue() const
{
	return  A[0][0] * A[1][1] - A[0][1] * A[1][0];
}

double CMatrix2D::GetValue(double d00, double d01,
	double d10, double d11)
{
	return  d00 * d11 - d01 * d10;
}

CMatrix2D CMatrix2D::CreateMirrorMatrix(VECTOR2D v)
{
	double len = ((CVector2D)v).GetLength();
	CMatrix2D matrix;
	matrix.A[0][0] = (v.x*v.x - v.y * v.y) / len;
	matrix.A[0][1] = (v.x*v.y)*2.0 / len;
	matrix.A[1][0] = (v.x*v.y)*2.0 / len;
	matrix.A[1][1] = matrix.A[0][0];
	return matrix;
}

CMatrix2D CMatrix2D::CreateRotateMatrix(double da)
{
	CMatrix2D m;
	m.A[0][0] = m.A[1][1] = cos(da);
	m.A[0][1] = sin(da);
	m.A[1][0] = -sin(da);
	return m;
}

CMatrix2D CMatrix2D::CreateScaleMatrix(double dScale)
{
	CMatrix2D m;
	m.A[0][0] = m.A[1][1] = dScale;
	return m;
}

CMatrix2D CMatrix2D::CreateTransfMatrix(VECTOR2D vec)
{
	CMatrix2D m;
	m.A[2][0] = vec.x;
	m.A[2][1] = vec.y;
	return m;
}
